﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.PC.收货管理
{
    /// <summary>
    /// 收货管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class ReceivingBillController : BaseController
    {
        private readonly IReceivingBillService receivingBillService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="receivingBillService"></param>
        public ReceivingBillController(IWebHostEnvironment environment, IReceivingBillService receivingBillService) : base(environment)
        {
            this.receivingBillService = receivingBillService;
        }

        /// <summary>
        /// 添加收货单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddReceivingBill(ReceivingBill m)
        {
            if (string.IsNullOrEmpty(m.BillNo))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            string message = "";
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool IsOK = receivingBillService.AddReceivingBill(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));
        }

        /// <summary>
        /// 收货单列表显示
        /// </summary>
        /// <param name="ordersn"></param>
        /// <param name="billtype"></param>
        /// <param name="materialkey"></param>
        /// <param name="status"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingBillList(string? ordersn = "",int? billtype = 0, string? materialkey = "", int status = 5, int pageIndex = 1, int pageSize = 10)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = receivingBillService.GetReceivingBillList(ordersn,billtype, materialkey, status, pageIndex, pageSize, out totalCount);

            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 删除收货单
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelReceivingBill(long rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = receivingBillService.DelReceivingBill(rid, out string message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "收货中或已完成的收货单不允许删除！", "500"));
        }
        /// <summary>
        /// 批量删除收货单
        /// </summary>
        /// <param name="ridList"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelReceivingBillList(long[] ridList)
        {
            if (ridList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = true;
            foreach (var rid in ridList)
            {
                bool isOK = receivingBillService.DelReceivingBill(Convert.ToInt32(rid), out string message);
                if (!isOK)
                {
                    IsOK = false;
                    break;
                }
            }
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "收货中或已完成的收货单不允许删除！", "500"));
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingBillById(long rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var mobj = receivingBillService.GetReceivingBillById(rid);
            if (mobj != null)
            {
                return Ok(BaseResult.Result(mobj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 根据物料编码获取收货单
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetReceivingBillByMaerialCode(string? code = "")
        {
            if (string.IsNullOrEmpty(code))
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            var obj = receivingBillService.GetReceivingBillByMaerialCode(code);

            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 修改收货单
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdReceivingBill(ReceivingBill m)
        {
            if (m.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(m.BillNo))
            {
                return Ok(BaseResult.Result("400", "单号不能为空！"));
            }
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            string message = "";
            bool IsOK = receivingBillService.UpdReceivingBill(m, out message);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 枚举入库单据类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmInBillType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmInBillType>();

            return Ok(list);
        }
    }
}
